﻿using System;
using System.Windows.Controls;
using System.Windows.Interactivity;
using FarseerGames.FarseerPhysics.Mathematics;
using GameLogic.Controls;
using Spritehand.FarseerHelper;
using Spritehand.PhysicsBehaviors;

namespace Marble.Phone
{
    public partial class MarbleGame : UserControl, IGameFeatures
    {
        public MarbleGame()
        {
            // Required to initialize variables
            InitializeComponent();

            this.Loaded += (s, e) =>
                {
                    // Find _environmentPhysics
                    BehaviorCollection behaviors = Interaction.GetBehaviors(_root);
                    foreach (var behavior in behaviors)
                    {
                        if (behavior is PhysicsControllerBehavior)
                        {
                            _environmentPhysics = behavior as PhysicsControllerBehavior;
                            break;
                        }
                    }

                    _physicsController = _root.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
            

                    if (_environmentPhysics != null)
                    {
                        _sensor.ValueChanged += (s1, e1) =>
                        {
                            int gravH = (int)(_sensor.X * 100);
                            int gravV = (int)(_sensor.Y * 100);

                            Dispatcher.BeginInvoke(() =>
                                {

                                    _gravHLabel.Text = _sensor.XSum.ToString();
                                    _gravVLabel.Text = _sensor.YSum.ToString();

                                    Vector2 v = new Vector2((float)_sensor.Y * -100,(float)_sensor.X * -100);
                                    foreach (var k in _physicsController.PhysicsObjects)
                                    {
                                        //_physicsController.PhysicsObjects[k.Key].BodyObject.ApplyImpulse(v);
                                    }
                                });
                        };
                    }
                };
        }
        
        PhysicsControllerMain _physicsController;


        public event EventHandler<ScoresChangedEventArgs> ScoresChanged;
        public event EventHandler<LivesChangedEventArgs> LivesChanged;
        public event EventHandler<TimeChangedEventArgs> TimeChanged;
        public event EventHandler<GameOverEventArgs> GameOver;


        public void OnScoresChanged(int scores)
        {

        }

        public void OnLivesChanged(int lives)
        {

        }

        public void Start()
        {

        }

        public void Stop()
        {

        }

        public void Pause()
        {

        }

        public void Resume()
        {

        }

        public bool IsTimeBased
        {
            get { return false; }
        }

        public bool IsLifeBased
        {
            get { return true; }
        }

        public string GameName
        {
            get { return "Marble Mania"; }
        }

        public string DeveloperName
        {
            get { return "Oliver Scheer"; }
        }

        public string CompanyName
        {
            get { return "Microsoft Deutschland GmbH"; }
        }

        public string Description
        {
            get { return string.Empty; }
        }

        public TimeSpan GameDuration
        {
            get { return new TimeSpan(); }
        }

        public Uri SettingsPage
        {
            get { return null; }
        }

        
    }
}